package com.ihc.app.webapp.action;

import org.springframework.security.AccessDeniedException;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.security.providers.UsernamePasswordAuthenticationToken;
import org.apache.struts2.ServletActionContext;
import com.ihc.app.Constants;
import com.ihc.app.model.Main;
import com.ihc.app.model.Pwp;
import com.ihc.app.model.PwpSupplemental;
import com.ihc.app.model.Shared;
import com.ihc.app.model.User;
import com.ihc.app.service.WellnessManager;
import com.ihc.app.service.WellnessProfileExistsException;
import com.ihc.app.webapp.util.RequestUtil;
import com.opensymphony.xwork2.Preparable;

import org.springframework.mail.MailException;

import javax.servlet.http.HttpServletResponse;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

//TODO: Finish wellness forms
//TODO: Figure out how to generate primary keys/unique index for tables according to wellsource design
//TODO: Decide weather to create another primary key column for Wellsource MySQL tables
//TODO: EDIT Wellness profile
//TODO: VIEW Wellness profile
//TODO: Display Profiles by company -- ADMIN only
//TODO: Create company profile -- ADMIN Only -- Description: Health Provider creates company profile, puts in e-mail address of all people whom are part of company they are sent an e-mail invite for site they will use generated company number to sign-up

/**
 * Action to allow new users to sign up.
 */
public class WellnessProfileAction extends BaseAction implements Preparable{
    private static final long serialVersionUID = 6558317334878272308L;
    private WellnessManager wellnessManager;
    private String cancel;
    private Main main;
    private Pwp pwp;
    private PwpSupplemental pwpSupplemental;
    private Shared shared;
    private User user;
    private Map<String, String> oneToTen;

    //main data access variables
    private String firstname;
    private String lastname;
    private String address;
    private String state;
    private String zipcode;
    private String phone;
    private String phone2;
    private String email;
    private short race;
    private String companyname;
    private String physicianname;
    private String healthplanname;
    private String birthdate;
    private short gender;
    private String clientid;
    private String groupnumber;
    
       // PWP data access variables
    private Timestamp recorddate;
    private short age;
    private double weight;
    private double height;
    private short framesize;
    private short socialactivity;
    private short strengthexercise;
    private short stretchingexercise;
    private short exercisedays;
    private short chronicpain;
    private short breakfast;
    private short healthlimitations;
    private short snacking;
	private short salt;
	private short emotionalproblems;
	private short lifting;
	private short physicalexam;
	private short drinkinganddriving;
	private short womenpregnant;
	private short exampap;
	private short exammammogram;
	private short womengivenbirth;
	private short womenmenopause;
	private short womenbreastexam;
	private short medestrogen;
	private short sickdays;
	private short exambowel;
	private short examdental;
	private short examimmunizationsflu;
	private short systolicbp;
	private short diastolicbp;
	private double totalcholesterol;
	private double hdlcholesterol;
	private double glucose;
	private double triglycerides;
	private double waistgirth;
	private double hipgirth; 
	private short bodycompmethodpwp;
	private short sumofskinfolds;
	private double knownbodyfat;
	private double hemoglobina1c;
	private short fatintake;
	private short breads;
	private short fruitsandvegetables;
	private short drinksperweek;
	private short drugs;
	private short smokingstatus;
	private short chewingtobacco;
	private short copingstatus;
	private short strsigtense;
	private short strsignotgettingalong;
	private short strsignopleasure;
	private short strsigunabletostop;
	private short strsiganger;
	private short feelhappy;
	private short feelcalm;
	private short feelblue;
	private short feelrelax;
	private short feelenergy;
	private short feelworthless;
	private short sleep;
	private short jobsatisfaction;
	private short seatbelts;
	private short sunprotection;
    // PWP Supplemental data access variables
	private short hlthviewseriousproblem;
	private short hlthviewgetsickeasier;
	private short hlthviewseedoctor;
	private short hlthviewamhealthy;
	private short hlthviewpessimist;
	private short hlthviewexpectedhealth;
	private short perceivedhlthoverall;
	private short fhxcolorectalcancer;
	private short fhxhighbp;
	private short fhxhighcholesterol;
	private short fhxchd;
	private short fhxbreastcancer;
	private short fhxdiabetes;
	
	private short phxasthma;
	private short phxhighcholesterol;
	private short phxdiabetes;
	private short phxbowelpolyps;
	private short phxstroke;
	private short phxbronchitis;
	private short phxsciatica;
	private short phxheartattack;
	private short phxhighbp;
	private short phxcancernonskin;
	
	private short symchestpain;
	private short symshortnessofbreath;
	private short symdizziness;
	private short symnumbnessortingling;
	private short symurinationorthirst;
	private short symbackpain;
	
	private short limitclimbingstairs;
	private short limitwalkingblocks;
	private short limitliftinggroceries;
	
	private short changestatus;
	private short changestatusnutrition;
	private short changestatusweight;
	private short changestatusalcohol;
	private short changestatusstress;
	private short changestatussmoke;
	private short changestatusexercise;
	
	private short noconsent;
	
	private short optional1;
	private short optional2;
	private short optional3;
	private short optional4;
	private short optional5;
	private short optional6;
	private short optional7;
	private short optional8;
	private short optional9;
	private short optional10;
	private short optional11;
	private short optional12;
	
	private short hiquitsmoking;
	private short hiweightmgt;
	private short hiaerobics;
	private short hiwalking;
	private short hijogging;
	private short hifitnesseval;
	private short hinutrition;
	private short hicholesterol;
	private short hibp;
	private short hicoronaryrisk;
	private short hialcoholdrug;
	private short hihealthyback;
	private short hiselfcare;
	private short histressmgt;
	private short hicpr;
	private short hifirstaid;
	private short hihealtheval;
	private short hiwomenshealth;
	private short hidiabetes;
	private short hicommunication;

    /**
     *  Getters and Setters for OGNL access variables
     */
    public Map<String, String> getOneToTen() {
        return oneToTen;
    }

    public void setOneToTen(Map<String, String> oneToTen) {
        this.oneToTen = oneToTen;
    }
    
    public void setCancel(String cancel) {
        this.cancel = cancel;
    }

    public Main getMain() {
        return main;
    }

    public void setMain(Main main) {
        this.main = main;
    }

    public Pwp getPwp() {
        return pwp;
    }

    public void setPwp(Pwp pwp) {
        this.pwp = pwp;
    }

    public PwpSupplemental getPwpSupplemental() {
        return pwpSupplemental;
    }

    public void setPwpSupplemental(PwpSupplemental pwpSupplemental) {
        this.pwpSupplemental = pwpSupplemental;
    }

    public Shared getShared() {
        return shared;
    }

    public void setShared(Shared shared) {
        this.shared = shared;
    }

    /*
        pre-processing
     */
    public void prepare() throws Exception {
    	// TODO Auto-generated method stub
        if (!"".equals(getRequest().getRemoteUser())) {
            user = userManager.getUserByUsername(getRequest().getRemoteUser());
        }

        oneToTen = new HashMap();
        oneToTen.put("1","1");
        oneToTen.put("2","2");
        oneToTen.put("3","3");
        oneToTen.put("4","4");
        oneToTen.put("5","5");
        oneToTen.put("6","6");
        oneToTen.put("7","7");
        oneToTen.put("8","8");
        oneToTen.put("9","9");
        oneToTen.put("10","10");

        main = new Main();
        if(user.getLastName()!=null)main.setLastname(user.getLastName());
        if(user.getFirstName()!=null)main.setFirstname(user.getFirstName());
        if(user.getAddress().getCity()!=null)main.setCity(user.getAddress().getCity());
        if(user.getAddress().getProvince()!=null)main.setState(user.getAddress().getProvince());
        if(user.getAddress().getAddress()!=null)main.setAddress(user.getAddress().getAddress());
        if(user.getAddress().getPostalCode()!=null)main.setZipcode(user.getAddress().getPostalCode());
        if(user.getEmail()!=null)main.setEmail(user.getEmail());
        if(user.getPhoneNumber()!=null)main.setPhone(user.getPhoneNumber());
    }
    
    /*
    Injecting Managers
     */
    public void setWellnessManager(WellnessManager wellnessManager) {
        this.wellnessManager = wellnessManager;
    }

    /**
     * When method=GET, "input" is returned. Otherwise, "success" is returned.
     * @return cancel, input or success
     */
    public String execute() {
        if (cancel != null) {
            return CANCEL;
        }
        if (ServletActionContext.getRequest().getMethod().equals("GET")) {
            return INPUT;
        }
        return SUCCESS;
    }

    /**
     * Returns "input"
     * @return "input" by default
     */
    public String doDefault() {
        return INPUT;
    }

    /**
     * Save the wellness profile
     * @return success when good things happen
     * @throws Exception when bad things happen
     */
    public String saveMain() throws Exception {
        if(main==null)return INPUT;
        main.setId(user.getId());
        wellnessManager.saveMain(main);
        return SUCCESS;
    }
    public String savePwp() throws Exception {
    	if(pwp==null)return INPUT;
        pwp.setId(user.getId());
        wellnessManager.savePwp(pwp);
        return SUCCESS;
    }
    public String savePwpSupplemental() throws Exception {
        if(pwpSupplemental==null)return INPUT;
        pwpSupplemental.setId(user.getId());
        wellnessManager.savePwpSupplemental(pwpSupplemental);
        return SUCCESS;
    }
    public String saveShared() throws Exception {
        if(shared==null)return INPUT;
        shared.setId(user.getId());
        wellnessManager.saveShared(shared);
        return SUCCESS;
    }


}
